package com.yh.csx.crm.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.dao.CustomerMapper;
import com.yh.csx.crm.dao.ReportAccountAgeMapper;
import com.yh.csx.crm.dao.ReportBigSalemanMapper;
import com.yh.csx.crm.dao.ReportCreditWarningMapper;
import com.yh.csx.crm.dao.ReportFoodSaleMapper;
import com.yh.csx.crm.dao.ReportHomeSaleMapper;
import com.yh.csx.crm.dao.ReportShopSaleMapper;
import com.yh.csx.crm.dao.SysSalesPermissionMapper;
import com.yh.csx.crm.po.*;
import com.yh.csx.crm.qo.ClassRatioQo;
import com.yh.csx.crm.qo.CustomerListQo;
import com.yh.csx.crm.qo.ReportCreditWarningQo;
import com.yh.csx.crm.qo.ReprotAccountAgesQo;
import com.yh.csx.crm.service.request.ReprotAccountAgeReq;
import com.yh.csx.crm.service.response.AccountReceivableResp;
import com.yh.csx.crm.service.response.CreditWarningResp;
import com.yh.csx.crm.service.vo.FoodInfoResp;
import com.yh.csx.crm.service.vo.PageWrap;
import com.yh.csx.crm.service.vo.ReportBigSalemaResp;
import com.yh.csx.crm.service.vo.ReportGetChal;
import com.yh.csx.crm.service.vo.ReportGetCityNameResp;
import com.yh.csx.crm.service.vo.ReportGetProNameResp;
import com.yh.csx.crm.service.vo.ReportNewFormsResp;
import com.yh.csx.crm.service.vo.ShopCustlistsResp;
import com.yh.csx.crm.service.vo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportFormsService {
    @Autowired
    ReportHomeSaleMapper reportHomeSaleMapper;

    @Autowired
    ReportBigSalemanMapper reportBigSalemanMapper;

    @Autowired
    ReportFoodSaleMapper reportFoodSaleMapper;

    @Autowired
    ReportShopSaleMapper reportShopSaleMapper;

    @Autowired
    SysSalesPermissionMapper sysSalesPermissionMapper;

    @Autowired
    ReportCreditWarningMapper reportCreditWarningMapper;

    @Autowired
    ReportAccountAgeMapper reprotAccountAgeMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    SaleService saleService;

    /**
     * 获取应收账款列表
     *
     * @param req
     * @return
     */
    public PageWrap getAccountAgeList(User user, ReprotAccountAgeReq req) {
        ReprotAccountAgesQo qo = BeanUtils.convert(ReprotAccountAgesQo.class, req);

        CustomerListQo customerListQo = new CustomerListQo();
        customerListQo.setSubordinateUserIdList(saleService.getPermitUserIds(user.getUserId()));

        List<Customer> customerList = customerMapper.selectByExample(customerListQo);
        List<String> custIds = new ArrayList<>();
        Map<String, Customer> customerMap = new HashMap<>();
        customerList.forEach(c->{
            if (StringUtils.isNotBlank(c.getCustomerNumber())) {
                custIds.add(c.getCustomerNumber());
                customerMap.put(c.getCustomerNumber(), c);
            }
        });
        if (custIds.isEmpty()) {
            custIds.add(user.getUserId().toString());
        }
        qo.setCustIds(custIds);

        Page<Object> page = PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<AccountReceivableResp> reprotAccountAges = BeanUtils.convertList(AccountReceivableResp.class, reprotAccountAgeMapper.selectList(qo));

        reprotAccountAges.forEach(o -> {
            Customer customer = customerMap.get(o.getCustId());
            o.setPhone(customer.getPhone());
            o.setSalesUserName(customer.getSalesUserName());
        });
        PageWrap pageWrap = new PageWrap<>();
        pageWrap.setValue(page);
        pageWrap.setResult(reprotAccountAges);
        return pageWrap;

    }

    /**
     * 信控预警
     *
     * @param qo
     * @return
     */
    public PageWrap getCreditWarningList(User user, ReportCreditWarningQo qo) {
        qo.setSalesUserIds(saleService.getPermitUserIds(user.getUserId()));
        PageHelper.startPage(1, 10);
        List<ReportCreditWarning> reportCreditWarnings = reportCreditWarningMapper.selectList(qo);
        return PageWrap.wrap(reportCreditWarnings, vo ->
                BeanUtils.convert(CreditWarningResp.class, vo)
        );
    }


    /**
     * 首页加载时调用
     *
     * @param
     * @return
     */
    public ReportNewFormsResp getCustomer(ReportHomeSale reportHomeSale) {
        //获取指标上半部分详情
        ReportHomeSale appHo = reportHomeSaleMapper.selectByAll(reportHomeSale);

        //获取指标下半部分详情
        ReportCountData appHoNext = reportHomeSaleMapper.selectByAllNext(reportHomeSale);

        //拿到业绩详情
        List<ReportHomeSale> list = reportHomeSaleMapper.selectDetailedInfo(reportHomeSale);

        //拿到品类占比
        List<ReportClassRatio> saleRationList = reportHomeSaleMapper.selectClass(reportHomeSale);

        //拿到7天数据
        List<ReportSale7day> sale7days = reportHomeSaleMapper.select7day();

        //拿到6个月数据
        List<ReportSale6month> sale6months = reportHomeSaleMapper.select6month();


        ReportNewFormsResp reportNewFormsResp = new ReportNewFormsResp();
        reportNewFormsResp.setAppHo(appHo);
        reportNewFormsResp.setAppHoNext(appHoNext);
        reportNewFormsResp.setList(list);
        reportNewFormsResp.setSaleRationList(saleRationList);
        reportNewFormsResp.setSale7days(sale7days);
        reportNewFormsResp.setSale6months(sale6months);

        return reportNewFormsResp;

    }

    /**
     * 获取指标详情
     *
     * @param reportHomeSales
     * @return
     */
    public ReportHomeSale getResultInfo(ReportHomeSale reportHomeSales) {

        ReportHomeSale reportHomeSale = reportHomeSaleMapper.selectByAll(reportHomeSales);

        return reportHomeSale;
    }

    /**
     * 获得品类占比详情
     *
     * @param qo
     * @return
     **/

    public List<ReportClassRatio> getClassSaleRationInfo(ClassRatioQo qo) {
        return reportHomeSaleMapper.selectClassInfo(qo);
    }

    /**
     * 获得业绩详情
     *
     * @return
     */

    public List<ReportHomeSale> getAppHomeSaleInfo(ReportHomeSale reportHomeSale) {
        return reportHomeSaleMapper.selectBaseDetailedInfo(reportHomeSale);
    }

    /**
     * 获得城市业绩详情
     *
     * @return
     */

    public List<ReportHomeSale> getCityResult(ReportHomeSale reportHomeSale) {
        return reportHomeSaleMapper.getCityInfo(reportHomeSale.getDimDate(), reportHomeSale.getProvinceName(), "全省", reportHomeSale.getChannelsale());
    }

    /**
     * 获得大客户信息
     *
     * @param reportBigSaleman
     * @return
     */
    public ReportBigSalemaResp getBigSaleman(ReportBigSaleman reportBigSaleman) {

        List<ReportBigSaleman> bigSalemanInfo = reportBigSalemanMapper.getBigSalemanInfo(reportBigSaleman);
        List<ReportBigSalemanCustlist> bigCutInfoList = reportBigSalemanMapper.getBigCutInfoList(reportBigSaleman);
        Integer i = 0;
        for (ReportBigSalemanCustlist re : bigCutInfoList) {
            Integer in = i += 1;
            re.setNumber(in);
        }
        ReportBigSalemaResp reportBigSalemaResp = new ReportBigSalemaResp();
        reportBigSalemaResp.setBigSalemanInfo(bigSalemanInfo);
        reportBigSalemaResp.setBigCutInfoList(bigCutInfoList);
        return reportBigSalemaResp;
    }

    /**
     * 获得餐饮信息
     *
     * @param reportFoodSale
     * @return
     */
    public FoodInfoResp getFoodInfo(ReportFoodSale reportFoodSale) {
        List<ReportFoodSale> foodInfo = reportFoodSaleMapper.getFoodInfo(reportFoodSale);
        List<ReportFoodSaleCustlist> reportFoodSaleCustlists = reportFoodSaleMapper.saleFoodInfo(reportFoodSale);
        Integer i = 0;
        for (ReportFoodSaleCustlist re : reportFoodSaleCustlists) {
            Integer in = i += 1;
            re.setNumber(in);
        }
        FoodInfoResp foodInfoResp = new FoodInfoResp();
        foodInfoResp.setFoodInfo(foodInfo);
        foodInfoResp.setReportFoodSaleCustlists(reportFoodSaleCustlists);
        return foodInfoResp;
    }


    /**
     * 获得m段信息
     *
     * @param reportShopSale
     * @return
     */
    public ShopCustlistsResp getMInfo(ReportShopSale reportShopSale) {
        List<ReportShopSale> shopInfo = reportShopSaleMapper.getShopInfo(reportShopSale);
        List<ReportShopSaleCustlist> shopCustlists = reportShopSaleMapper.getShopListInfo(reportShopSale);
        Integer i = 0;
        for (ReportShopSaleCustlist re : shopCustlists) {
            Integer in = i += 1;
            re.setNumber(in);
        }
        ShopCustlistsResp shopCustlistsResp = new ShopCustlistsResp();
        shopCustlistsResp.setShopInfo(shopInfo);
        shopCustlistsResp.setShopCustlists(shopCustlists);
        return shopCustlistsResp;
    }

    /**
     * 获得渠道
     *
     * @return
     */
    public ReportGetChal getChannelsale() {
        List<String> list = reportHomeSaleMapper.getChannelsale();
        if (!list.contains("all")) {
            list.add(0, "all");
        }
        ReportGetChal reportGetChal = new ReportGetChal();
        reportGetChal.setList(list);

        return reportGetChal;
    }

    /**
     * 获得收益省名
     *
     * @return
     */
    public ReportGetProNameResp getProvinceName(Long userId) {
        List<String> list = reportHomeSaleMapper.getProvinceName(saleService.getPermitProvinceCodes(userId));
        list.remove("全国");
        list.add(0, "全国");
        ReportGetProNameResp reportGetProName = new ReportGetProNameResp();
        reportGetProName.setList(list);
        return reportGetProName;
    }

    /**
     * 根据省名获得城市名
     *
     * @param provinceName
     * @return
     */
    public ReportGetCityNameResp getCityName(String provinceName) {
        List<String> list = reportHomeSaleMapper.getCityName(provinceName);
        if (!list.contains("全省")) {
            list.add(0, "全省");
        }
        ReportGetCityNameResp reportGetCityNameResp = new ReportGetCityNameResp();
        reportGetCityNameResp.setList(list);
        return reportGetCityNameResp;
    }

    /**
     * 判断是否有权限
     *
     * @return
     */
    public Boolean getPower(Long userId) {
        String string = sysSalesPermissionMapper.selectPosition(userId);
        if (StringUtils.isNotBlank(string) && "SALES".equals(string)) {
            return true;
        }
        return false;
    }
}
